﻿using EasyAdmin.Domain.Entities.Applications;
using EasyCore.DataValidation;
using EasyCore.ObjectMapper;
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyAdmin.Application.Contracts.Books.Dtos
{
    [MapTo(typeof(Book))]
    public class CreateUpdateBookDto
    {
        /// <summary>
        /// 唯一标识
        /// </summary>
        public long? Id { get; set; }
        public int ab { get; set; }

        /// <summary>
        /// 书籍名称
        /// </summary>
        [DisplayName("书籍名称")]
        [Required(ErrorMessage ="{0}为必填字段")]
        public string Name { get; set; }

        /// <summary>
        /// 作者
        /// </summary>
        [AllowNull]
        public string Auther { get; set; } = null!;

        [Required]
        [DataType(DataType.Date)]
        public DateTime PublishDate { get; set; } = DateTime.Now;

        [Required]
        [DisplayName("价格")]
        public float Price { get; set; }
    }

    public class CreateUpdateBookDtoValidator : AbstractValidator<CreateUpdateBookDto>
    {
        public CreateUpdateBookDtoValidator()
        {
            RuleFor(x => x.Name).NotNull().WithMessage("书籍名称不能为空").Length(1, 20).WithMessage("书籍名称的长度为1至20个字符");
            RuleFor(X => X.Price).GreaterThanOrEqualTo(0).WithMessage("书籍的价格要大于等于0");
            RuleFor(x => x.PublishDate).Must(x => x <= DateTime.Now).WithMessage("出版时间必需小于当前时间");
            RuleFor(x => x.Auther).NotNull().WithMessage("作者不能为空");
        }
    }
}
